<?php 

/**
 * 
 * 微博开放API操作类
 * @author songerzhou
 *
 */

class weibo 
{
	private $akey;
	private $skey;
	private $base;
	private $curl;
	private $user = array();
	private $getdata = array();
	private $postdata = array();
	
	/**
	 * 类构造函数，初始化akey,skey,curl
	 * @param string $akey
	 * @param string $skey
	 */
	function __construct( $akey , $skey = '' ) 
	{
		$this->akey = $akey;
		$this->skey = $skey;
		$this->base = 'http://api.t.sina.com.cn/';
		$this->curl = curl_init();
		curl_setopt( $this->curl , CURLOPT_RETURNTRANSFER, true); 
		
		$this->postInit();
		
	}
	
	/**
	 * get、post数组初始化
	 */
	function postInit()
	{
		$this->getdata = array();
		$this->postdata = array( 'source' => $this->akey );
	
	}

	/**
	 * 设置用户名和密码
	 * @param string $name
	 * @param string $pass
	 */
	function setUser( $name , $pass ) 
	{
		$this->user['oname'] = $name;
		$this->user['opass'] = $pass;
		
		$this->user['name'] = $name;
		$this->user['pass']  = $pass;
		curl_setopt( $this->curl , CURLOPT_USERPWD , "$name:$pass" );
	}
	
	/**
	 * 
	 * 根据数组生成请求字符串
	 * @param array $data_array
	 */
	function generationRequest($data_array = array(),$get = true)
	{
		if(!empty($data_array)) {
			if($get)
				$request_string = '?';
			$request_array = array();
			foreach ($data_array as $var => $value) {
				$request_array[] = $var . '=' . $value;
			}
			$request_string .= implode("&", $request_array);
			unset($request_array);
		} else {
			$request_string = '';
		}
		return $request_string;
	}
	
	/**
	 * 获取最新更新的公共微博消息
	 */
	function public_timeline()
	{
		return $this->call_method( 'statuses' , 'public_timeline' );
	}
	
	/**
	 * 
	 * 获取当前用户所关注用户的最新微博信息，$since_id<ID<$max_id
	 * @param 开始ID $since_id
	 * @param 终于ID $max_id
	 * @param 读取数 $count
	 * @param 页数 $page
	 */
	function friends_timeline( $since_id = 0, $max_id = 0, $count = 10, $page = 1 )
	{
		if($since_id)
			$this->getdata['since_id'] = $since_id;
		if($max_id)
			$this->getdata['max_id'] = $max_id;
		$this->getdata['count'] = $count;
		$this->getdata['page'] = $page;

		return $this->call_method( 'statuses' , 'friends_timeline' );
	}
	
	function user_timeline( $name ) 
	{
		$this->getdata['screen_name'] = urlencode( $name );
		
		return $this->call_method( 'statuses' , 'user_timeline' );
	}
	
	/**
	 * 
	 * 获取@当前用户的微博列表，$since_id<ID<$max_id
	 * @param 开始ID $since_id
	 * @param 终于ID $max_id
	 * @param 读取数 $count
	 * @param 页数 $page
	 */
	function mentions( $since_id = 0, $max_id = 0, $count = 10, $page = 1 ) 
	{
		if($since_id)
			$this->getdata['since_id'] = $since_id;
		if($max_id)
			$this->getdata['max_id'] = $max_id;
		$this->getdata['count'] = $count;
		$this->getdata['page'] = $page;
		
		return $this->call_method( 'statuses' , 'mentions' );
	}
	
	//获取当前用户发送及收到的评论列表
	function comments_timeline( $since_id = 0, $max_id = 0, $count = 10, $page = 1 ) 
	{
		if($since_id)
			$this->getdata['since_id'] = $since_id;
		if($max_id)
			$this->getdata['max_id'] = $max_id;
		$this->getdata['count'] = $count;
		$this->getdata['page'] = $page;
		
		return $this->call_method( 'statuses' , 'comments_timeline' );
	}
	
	//获取当前用户发出的评论
	function comments_by_me( $since_id = 0, $max_id = 0, $count = 10, $page = 1 )
	{
		if($since_id)
			$this->getdata['since_id'] = $since_id;
		if($max_id)
			$this->getdata['max_id'] = $max_id;
		$this->getdata['count'] = $count;
		$this->getdata['page'] = $page;
		
		return $this->call_method( 'statuses' , 'comments_by_me' );
	}
	
	//获取当前用户收到的评论
	function comments_to_me( $since_id = 0, $max_id = 0, $count = 10, $page = 1 )
	{
		if($since_id)
			$this->getdata['since_id'] = $since_id;
		if($max_id)
			$this->getdata['max_id'] = $max_id;
		$this->getdata['count'] = $count;
		$this->getdata['page'] = $page;
		
		return $this->call_method( 'statuses' , 'comments_to_me' );
	}
	
	//获取指定微博的评论列表
	function comments( $tid , $count = 10 , $page = 1 )
	{
		$this->getdata['id'] = $tid;
		$this->getdata['count'] = $count;
		$this->getdata['page'] = $page;
		
		return $this->call_method( 'statuses' , 'comments' );
	}
	
	//批量获取一组微博的评论数及转发数
	function counts( $tids_array )
	{
		$tids = implode(",", $tids_array);

		$this->getdata['ids'] = $tids;
		
		return $this->call_method( 'statuses' , 'counts' );
	}
	
	//获取当前用户未读消息数
	function unread($since_id = 0, $with_new_status = 0)
	{
		if($since_id)
			$this->getdata['since_id'] = $since_id;
		$this->getdata['with_new_status'] = $with_new_status;
		
		return $this->call_method( 'statuses' , 'unread' );
	}
	
	//未读消息数清零接口
	function reset_count($type = 0)
	{
		$this->getdata['type'] = $type;
		
		return $this->call_method( 'statuses' , 'reset_count' );
	}
	
	//根据ID获取单条微博信息内容 
	function show( $tid )
	{
		return $this->call_method( 'statuses' , 'show/' . $tid  );
	}
	
	//删除一条微博信息 
	function destroy( $tid )
	{
		//curl_setopt( $this->curl , CURLOPT_CUSTOMREQUEST, "DELETE"); 		
		return $this->call_method( 'statuses' , 'destroy/' . $tid  );
	}
	
	//转发一条微博信息（可加评论） 
	function repost( $tid , $status )
	{
		$this->postdata['id'] = $tid;
		$this->postdata['status'] = urlencode($status);
		
		return $this->call_method( 'statuses' , 'repost' );
	}
	
	//发布一条微博信息
	function update( $status )
	{
		$this->postdata['status'] = urlencode($status);
		
		return $this->call_method( 'statuses' , 'update' );
	}
	
	//上传图片并发布一条微博信息
	function upload( $status , $file )
	{
		$boundary = uniqid('------------------');
		$MPboundary = '--'.$boundary;
		$endMPboundary = $MPboundary. '--';
		
		$multipartbody .= $MPboundary . "\r\n";
		$multipartbody .= 'Content-Disposition: form-data; name="pic"; filename="wiki.jpg"'. "\r\n";
		$multipartbody .= 'Content-Type: image/jpg'. "\r\n\r\n";
		$multipartbody .= $file. "\r\n";

		$k = "source";
		// 这里改成 appkey
		$v = $this->akey;
		$multipartbody .= $MPboundary . "\r\n";
		$multipartbody.='content-disposition: form-data; name="'.$k."\"\r\n\r\n";
		$multipartbody.=$v."\r\n";
		
		$k = "status";
		$v = $status;
		$multipartbody .= $MPboundary . "\r\n";
		$multipartbody.='content-disposition: form-data; name="'.$k."\"\r\n\r\n";
		$multipartbody.=$v."\r\n";
		$multipartbody .= "\r\n". $endMPboundary;
		
		curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt( $this->curl , CURLOPT_POST, 1 );
		curl_setopt( $this->curl , CURLOPT_POSTFIELDS , $multipartbody );
		$url = 'http://api.t.sina.com.cn/statuses/upload.json' ;
		curl_setopt( $this->curl , CURLOPT_USERPWD , $this->user['oname'] . ":" . $this->user['opass'] );
		
		$header_array = array("Content-Type: multipart/form-data; boundary=$boundary" , "Expect: ");

        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header_array ); 
        curl_setopt($this->curl, CURLOPT_URL, $url );
		curl_setopt($this->curl, CURLOPT_HEADER , true );
		curl_setopt($this->curl, CURLINFO_HEADER_OUT , true );
		
		$info = curl_exec( $this->curl );
		
		//print_r( curl_getinfo( $this->curl ) );
		
		return json_decode( $info , true);
		// =================================================
		
		//return $this->call_method( 'statuses' , 'upload' );
	}
	
	//对一条微博信息进行评论 
	function send_comment( $tid , $comment )
	{
		$this->postdata['id'] = $tid;
		$this->postdata['comment'] = urlencode($comment);
		
		return $this->call_method( 'statuses' , 'comment' );
	}
	
	//回复微博评论信息
	function reply( $tid, $cid, $comment )
	{
		$this->postdata['id'] = $tid;
		$this->postdata['cid'] = $cid;
		$this->postdata['comment'] = urlencode($comment);
		
		return $this->call_method( 'statuses' , 'comment' );
	}
	
	//删除当前用户的微博评论信息 
	function remove_comment( $cid )
	{
		return $this->call_method( 'statuses' , 'comment_destroy/'.$cid  );
	}
	
	//获取用户粉丝对象uid列表
	function followers_ids( $cid , $count = 500 )
	{
		$this->getdata['count'] = $count;
		
		return $this->call_method( 'followers' , 'ids/'.$cid );
	}
	
	//获取两个用户关系的详细情况 
	function friendships_show( $cid )
	{
		$this->getdata['target_id'] = $cid;
		
		return $this->call_method( 'friendships' , 'show' );
	}
	
	//关注某用户
	function friendships_create( $cid )
	{	
		return $this->call_method( 'friendships' , 'create/'.$cid );
	}
	
	//发送一条私信 
	function direct_messages( $cid , $text )
	{
		$this->postdata['user_id'] = $cid;
		$this->postdata['text'] = urlencode($text);
		return $this->call_method( 'direct_messages' , '/new'  );
	}
	
	//获取当前用户的收藏列表 
	function get_favorites( $page = 1 ) 
    {
    	$this->getdata['page'] = $page;
    	
        return $this->call_method( '' , 'favorites' );
    } 

    //添加收藏
    function add_to_favorites( $sid ) 
    { 
        $this->postdata['id'] = $sid;
        
        return $this->call_method( 'favorites' , 'create' );
    } 

    //删除当前用户收藏的微博信息
    function remove_from_favorites( $sid ) 
    { 
        $this->postdata['id'] = $sid;
        
        return $this->call_method( 'favorites' , 'destroy' ); 
    } 
    
    //验证当前用户身份是否合法
    function verify_credentials() 
    { 
        return $this->call_method( 'account' , 'verify_credentials' );
    } 
    
	function call_method( $method , $action ) 
	{
		/*
		var_dump($this->base . $method . '/' . $action . '.json' . $this->generationRequest($this->getdata, true));
		echo "<br/>\n";
		var_dump($this->generationRequest($this->postdata, false ));
		echo "<br/>\n";
		$this->postInit();
		return;
		*/
		
		curl_setopt( $this->curl , CURLOPT_POSTFIELDS , $this->generationRequest( $this->postdata , false ) );
		$url = $this->base . $method . '/' . $action . '.json' . $this->generationRequest( $this->getdata , true ) ;
		curl_setopt($this->curl , CURLOPT_URL , $url );
		
		$ret = curl_exec( $this->curl );
		// clean post data
		$this->postInit();
		
		return json_decode( $ret , true);
	}
	
	function __destruct ()
	{
		curl_close($this->curl);
	}
	
	//function 

}
?>